// pages/newgoods/hotgoods.js
const interfaces = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHot: "",
    listData: [],
    currentSortType: "default",
    currentSortOrder: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const self = this;
    wx.request({
      url: interfaces.newgoods,
      data: {
        isHot: Number(options.isHot),
        order: self.data.currentSortOrder
      },
      success(res) {
        console.log(res)
        self.setData({
          listData: res.data.data
        })
      }
    })
  },
  openSort(e) {
    let uid = e.currentTarget.dataset.uid;
    switch (uid) {
      case 'classify':
        this.setData({
          currentSortType: 'category',
          currentSortOrder: ""
        })
        this.getData();
        break;
      case 'tariff':
        let tmp = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmp = 'desc'
        }
        this.setData({
          currentSortType: "price",
          currentSortOrder: tmp
        })
        this.getData();
        break;
      default:
        this.setData({
          currentSortType: "default",
          currentSortOrder: ""
        });
        this.getData();
    }
  },
  getData() {
    const self = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: interfaces.newgoods,
      data: {
        isHot: 1,
        order: self.data.currentSortOrder
      },
      success(res) {
        //console.log(res)
        wx.hideLoading();
        self.setData({
          listData: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})